<?php
namespace App\Http\Controllers\admin_system;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use App\Common\MobileMassege;
use DB;

class SystemController extends Controller{

    /**
     * 发送书籍上架信息页面
     * */
    public function send_publish_book_message(Request $request){
        $message = $request->session()->get('message','');
        return view('admin_system/system/send-publish-book-message',['message'=>$message]);
    }

    /*
     * 发送书籍上架信息页面动作
     * */
    public function send_publish_book_message_action(Request $request){

        $message    = $request->input('message');
        $isTest     = $request->input('isTest');
        $testMobile = $request->input('testMobile');
        $request->session()->flash('message',$message);
        $GLOBALS['mobileMassege']  = new MobileMassege();
        $GLOBALS['mobileNumber']   = 0;
        $GLOBALS['message']        = $message;

        if((int)$isTest === 1 && $testMobile ){

            $GLOBALS['mobileMassege']->mobile = $testMobile;
            $res = $GLOBALS['mobileMassege']->send_msg($message);
            if($res){
                $GLOBALS['mobileNumber']    = 1;
            }else{
                $GLOBALS['mobileNumber']    = 0;
            }

        }else{
            $condition = [
                ['States','=',1],
                ['DeleteMark','=',1],
                //['UserName','=','15000606942']
            ];

            DB::table('memberinfo')->where($condition)->select('UserName')->chunk(1000,function($users){
                $mobiles = [];
                foreach($users as $user){
                    if($user->UserName){
                        array_push($mobiles,$user->UserName);
                    }
                }
                $msg = $GLOBALS['message'];
                if(count($mobiles) > 0) {
                    $GLOBALS['mobileNumber'] += count($mobiles);
                    $GLOBALS['mobileMassege']->mass_massege($msg,$mobiles);
                }
            });
        }


        return view('admin_system/system/send-publish-book-message-action',["totalNumber"=>$GLOBALS['mobileNumber']]);

    }



}